import React from "react";
import { Link, Route, Switch } from "react-router-dom";

// 嵌套路由就是在其中的一个子路由里面继续使用Link和Route组件
// 如果一个路由有下一级的路由，那么这个路由就不要加exact属性

const Hot = () => {
  return <div>这是热映页面</div>;
};

const Cinema = () => {
  return <div>这是影院页面</div>;
};

class Home extends React.Component {
  render() {
    // 当一个组件变成路由组件后，那么props里面会自动有路由信息
    // console.log(this);
    // 二次解构
    const {
      match: { path },
    } = this.props;

    return (
      <>
        <div>home页面</div>
        <ul>
          <li>
            <Link to={`${path}/hot`}>热映</Link>
          </li>
          <li>
            <Link to={`${path}/cinema`}>影院</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path={`${path}/hot`} component={Hot}></Route>
          <Route path={`${path}/cinema`} component={Cinema}></Route>
        </Switch>
      </>
    );
  }
}

const About = () => {
  return <div>about页面</div>;
};

const App = () => {
  return (
    <>
      <h2>react-router-v5-路由嵌套</h2>
      <ul>
        <li>
          <Link to="/home">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
      </ul>

      <hr />

      <Switch>
        <Route path="/home" component={Home}></Route>
        <Route path="/about" component={About}></Route>
      </Switch>
    </>
  );
};

export default App;
